package com.ruoyi.project.jiaqin.followmember.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.followmember.domain.JiaqinFollowMember;
import com.ruoyi.project.jiaqin.followmember.mapper.JiaqinFollowMemberMapper;
import com.ruoyi.project.jiaqin.followmember.service.IJiaqinFollowMemberService;
import com.ruoyi.project.system.member.domain.MemberFollow;
import com.ruoyi.project.system.member.mapper.MemberMapper;
import com.ruoyi.project.system.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 用户关注Service业务层处理
 *
 * @author admin
 * @date 2021-01-04
 */
@Service
public class JiaqinFollowMemberServiceImpl implements IJiaqinFollowMemberService
{
    @Autowired
    private JiaqinFollowMemberMapper jiaqinFollowMemberMapper;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 查询用户关注
     *
     * @param id 用户关注ID
     * @return 用户关注
     */
    @Override
    public JiaqinFollowMember selectJiaqinFollowMemberById(String id)
    {
        return jiaqinFollowMemberMapper.selectJiaqinFollowMemberById(id);
    }

    @Override
    public JiaqinFollowMember selectJiaqinFollowMemberByMemberIdAndToMemberId(String memberId, String toMemberId) {
        return jiaqinFollowMemberMapper.selectJiaqinFollowMemberByMemberIdAndToMemberId(memberId,toMemberId);
    }

    @Override
    public PageInfo<JiaqinFollowMember> selectJiaqinFollowMemberByMemberId(Integer pageNum, Integer pageSize, String memberId) {
        PageHelper.startPage(pageNum,pageSize);
        List<JiaqinFollowMember> list = jiaqinFollowMemberMapper.selectJiaqinFollowMemberByMemberId(memberId);
        if(list!=null&&list.size()!=0){
            for (JiaqinFollowMember j: list) {
                //关联被关注用户信息
                MemberFollow memberFollow = memberMapper.selectMemberFollowById(j.getFollowMemberId());
                j.setMemberFollow(memberFollow);
                j.setTotal(list.size());
                // 备注
                if (!j.getRemarks().equals("0")) {
                    memberFollow.setUsername(j.getRemarks());
                }
                // 距离
//                Member member1 = memberMapper.selectMemberById(memberId);
//                Member member2 = memberMapper.selectMemberById(j.getFollowMemberId());
//                if (member1 != null && member2 != null) {
//                    double distance = DistanceUtils.getDistance(member1.getLongitude().doubleValue(), member1.getLatitude().doubleValue()
//                            , member2.getLongitude().doubleValue(), member2.getLatitude().doubleValue());
//                    DecimalFormat df = new DecimalFormat("######0.0");
//                    df.format(distance);
//                    j.setDistance(df.format(distance));
//                } else {
//                    j.setDistance("0.1");
//                }
            }
        }
        PageInfo<JiaqinFollowMember> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    /**
     * 查询用户关注列表
     *
     * @param jiaqinFollowMember 用户关注
     * @return 用户关注
     */
    @Override
    public List<JiaqinFollowMember> selectJiaqinFollowMemberList(JiaqinFollowMember jiaqinFollowMember)
    {
        return jiaqinFollowMemberMapper.selectJiaqinFollowMemberList(jiaqinFollowMember);
    }

    /**
     * 新增用户关注
     *
     * @param jiaqinFollowMember 用户关注
     * @return 结果
     */
    @Override
    public int insertJiaqinFollowMember(JiaqinFollowMember jiaqinFollowMember)
    {
        jiaqinFollowMember.setId(UUID.randomUUID().toString().replace("-",""));
        jiaqinFollowMember.setRemarks("0");
        return jiaqinFollowMemberMapper.insertJiaqinFollowMember(jiaqinFollowMember);
    }

    /**
     * 修改用户关注
     *
     * @param jiaqinFollowMember 用户关注
     * @return 结果
     */
    @Override
    public int updateJiaqinFollowMember(JiaqinFollowMember jiaqinFollowMember)
    {
        return jiaqinFollowMemberMapper.updateJiaqinFollowMember(jiaqinFollowMember);
    }

    /**
     * 删除用户关注对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinFollowMemberByIds(String ids)
    {
        return jiaqinFollowMemberMapper.deleteJiaqinFollowMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户关注信息
     *
     * @param id 用户关注ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinFollowMemberById(String id)
    {
        return jiaqinFollowMemberMapper.deleteJiaqinFollowMemberById(id);
    }

    @Autowired
    private IMemberService memberService;
    @Autowired
    private ICompanyService companyService;
    @Override
    public PageInfo<JiaqinFollowMember> getMyFans(Integer pageNum, Integer pageSize, String memberId) {
        PageHelper.startPage(pageNum,pageSize);
        List<JiaqinFollowMember> list = jiaqinFollowMemberMapper.getMyFans(memberId);
        if(list != null && list.size() != 0) {
            for (JiaqinFollowMember JFM: list) {
                //关联粉丝用户信息
                MemberFollow memberFollow = memberMapper.selectMemberFollowById(JFM.getMemberId());
                Company company = companyService.selectCompanyByMemberId(JFM.getMemberId());
                if (memberFollow != null) {
                    // 是否是企业：0-不是 1-是
                    if (company != null) {
                        memberFollow.setIsCompany(1);
                        memberFollow.setMemberCompanyId(company.getId());
                        memberFollow.setHeadpic(company.getCompanyAvatar());
                        memberFollow.setUsername(company.getCompanyName());
                    } else {
                        memberFollow.setIsCompany(0);
                        memberFollow.setMemberCompanyId(JFM.getMemberId());
                    }
                    JFM.setMemberFollow(memberFollow);
                }
            }
        }
        // 去除list中用户信息（memberFollow）为空的数据
//        for (int i = list.size()-1; i >= 0; i--) {
//            if (list.get(i).getMemberFollow() == null) {
//                list.remove(i);
//            }
//        }
        PageInfo<JiaqinFollowMember> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

}
